/**
*	This sample redirects TCP/UDP traffic to the specified SOCKS5 proxy.
**/

#include "stdafx.h"
#include <ws2tcpip.h>
#include <crtdbg.h>
#include <process.h>
#include <map>
#include <queue>
#include "nfapi.h"
#include "sync.h"
#include "UdpProxy.h"
#include "TcpProxy.h"
#include "utf8.h"
#include "UserMgr.h"
#include "utils.h"
#include "Events.h"
#include "DriveEvent.h"
#include "global.h"

using namespace nfapi;

// Change this string after renaming and registering the driver under different name
#define NFDRIVER_NAME "netfilter2"

int main(int argc, char *argv[])
{
    DriveEventHandler eh;
    NF_RULE rule;
    WSADATA wsaData;

    // This call is required for WSAAddressToString
    ::WSAStartup(MAKEWORD(2, 2), &wsaData);
#if 1
    int proxyAddressLen = NF_MAX_ADDRESS_LENGTH;
    WSAStringToAddress((LPSTR) "104.167.65.37:28553", AF_INET, NULL,
                       (LPSOCKADDR)g_proxyAddress, &proxyAddressLen);
    g_userName = "";
    g_userPassword = "";
#endif
    DbgPrint("Press enter to stop...\n\n");

    if (!eh.init())
    {
        DbgPrint("Failed to initialize the event handler");
        return -1;
    }

    // Initialize the library and start filtering thread
    if (nf_init(NFDRIVER_NAME, &eh) != NF_STATUS_SUCCESS)
    {
        DbgPrint("Failed to connect to driver");
        return -1;
    }

    memset(&rule, 0, sizeof(rule));
    rule.filteringFlag = NF_FILTER | NF_INDICATE_CONNECT_REQUESTS;
    nf_addRule(&rule, TRUE);

    // Wait for enter
    getchar();

    // Free the library
    nf_free();

    eh.free();

    ::WSACleanup();

    return 0;
}